/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.view.grid.editor;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reportgear.report.model.cell.value.FormulaObject;
import com.reportgear.report.model.cell.value.JsDataFieldObject;
import com.reportgear.report.model.cell.value.salary.SalaryDetailObject;
import com.reportgear.report.model.cell.value.salary.SalaryDocObject;
import com.reportgear.report.model.cell.value.salary.SalaryFormulaObject;
import com.reportgear.report.model.cell.value.salary.SalaryItemObject;
import com.reportgear.report.model.cell.value.salary.SalaryObject;
import com.reportgear.report.model.cell.value.salary.SalaryPayObject;
import com.reportgear.report.view.grid.editor.impl.FormulaEditor;
import com.reportgear.report.view.grid.editor.impl.GeneralCellEditor;
import com.reportgear.report.view.grid.editor.impl.JsDataFieldObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryDetailObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryDocObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryFormulaObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryItemObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryObjectEditor;
import com.reportgear.report.view.grid.editor.impl.SalaryPayObjectEditor;

/**
 * 单元格编辑器管理器
 * 
 * @version 1.0 2011-3-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public enum CellEditorManager {
	INSTANCE;

	private final Logger logger = LoggerFactory.getLogger(CellEditorManager.class.getName());
	// 编辑器缓存
	private Map<Class<?>, CellEditor> editors = new HashMap<Class<?>, CellEditor>();
	// Action缓存
	private Map<Class<?>, CellEditAction> actionMap = new HashMap<Class<?>, CellEditAction>();

	CellEditorManager() {
		// 注册编辑器
		registerCellEditor(String.class, new GeneralCellEditor());
		registerCellEditor(SalaryObject.class, new SalaryObjectEditor());
		registerCellEditor(SalaryItemObject.class, new SalaryItemObjectEditor());
		registerCellEditor(SalaryFormulaObject.class, new SalaryFormulaObjectEditor());
		registerCellEditor(SalaryDocObject.class, new SalaryDocObjectEditor());
		registerCellEditor(SalaryDetailObject.class, new SalaryDetailObjectEditor());
		registerCellEditor(SalaryPayObject.class, new SalaryPayObjectEditor());
		registerCellEditor(FormulaObject.class, new FormulaEditor());
		registerCellEditor(JsDataFieldObject.class, new JsDataFieldObjectEditor());

		// 注册Action
		registerCellAction("普通文本", String.class);
		registerCellAction("自动工资对象", SalaryObject.class);
		registerCellAction("自动工资明细对象", SalaryDetailObject.class);
		registerCellAction("薪酬项目", SalaryItemObject.class);
		registerCellAction("薪酬项目公式", SalaryFormulaObject.class);
		registerCellAction("薪酬档案", SalaryDocObject.class);

		// registerCellAction("工资对象", SalaryMainObject.class);
		registerCellAction("发放表对象", SalaryPayObject.class);

		registerCellAction("公式", FormulaObject.class);

		registerCellAction("履历信息", JsDataFieldObject.class);
	}

	/**
	 * 注册单元格编辑器
	 * 
	 * @param clazz
	 *            单元格类
	 * @param cellEditor
	 *            单元格编辑器
	 */
	private void registerCellEditor(Class<?> clazz, CellEditor cellEditor) {
		this.editors.put(clazz, cellEditor);
	}

	/**
	 * 注册单元格对象操作Action
	 * 
	 * @param name
	 *            名称
	 * @param clazz
	 *            类
	 */
	private void registerCellAction(String name, Class<?> clazz) {
		if (actionMap.get(clazz) != null) {
			logger.warn(clazz.getName() + " 已经注册!");
			return;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("注册单元格对象编辑Action：" + clazz.getName());
		}
		actionMap.put(clazz, new CellEditAction(name, clazz));
	}

	/**
	 * 根据类获取编辑器
	 * 
	 * @param clazz
	 *            类
	 * @return 编辑器
	 */
	public CellEditor getCellEditor(Class<?> clazz) {
		CellEditor editor = editors.get(clazz);

		if (editor == null) {
			return editors.get(String.class);
		} else {
			return editor;
		}
	}

	/**
	 * 获取单元格对象操作Action
	 * 
	 * @param clazz
	 *            对象类
	 * @return Action
	 */
	public CellEditAction getCellAction(Class<?> clazz) {
		CellEditAction action = actionMap.get(clazz);
		if (action == null) {
			throw new InternalError("单元格对象编辑操作：" + clazz.getName() + "没有注册!");
		}

		return action;
	}

	/**
	 * 根据对象获取编辑器
	 * 
	 * @param o
	 *            对象
	 * @return 编辑器
	 */
	public CellEditor getCellEditor(Object o) {
		Class<?> clazz = (o == null ? Object.class : o.getClass());

		return this.getCellEditor(clazz);
	}
}
